<template>
	<view class="address-container">
    <!-- 选择收货地址的按钮 -->
		<view class="goods-address-box" v-if="JSON.stringify(address) === '{}'">
		  <button type="primary" size="mini" class="address-btn" @click="chooseAddress">请选择收货地址</button>
		</view>
    <!-- 渲染收货地址的区域 -->
    <view class="address-info-box" v-else>
      <!-- 收货人和电话 -->
      <view class="row1">
        <view class="row1-username">
          收货人:<text>{{ address.userName }}</text>
        </view>
        <view class="row1-phone">
          电话:<text>{{ address.telNumber }}</text>
          <uni-icons type="arrowright" size="mini"></uni-icons>
        </view>
      </view>
      <!-- 详细地址 -->
      <view class="row2">
        <view class="row2-address">
          <view class="row2-left">
            收货地址:
          </view>
          <view class="row2-right">{{ addStr }}</view>
        </view>
      </view>
    </view>
    <view>
      <image src="../../static/cart_border@2x.png" class="address-img"></image>
    </view>
	</view>
</template>

<script>
  import { mapState, mapMutations } from 'vuex'
	export default { 
		data() {
			return {
				// 收货地址
        // address: {}
			};
		},
    computed: {
      ...mapState('m_user', ['address']),
      // 定义收货地址的详细地址的计算属性
      addStr () {
        if (!this.address.provinceName) return ''
        // 拼接省市区和详细地址的字符串返回给用户
        return this.address.provinceName + this.address.cityName + this.address.countyName  + this.address.detailInfo
      }
    },
    methods: {
      ...mapMutations('m_user', ['updateAddress']),
      // 选择收货地址
      async chooseAddress () {
        const [err, succ] = await uni.chooseAddress().catch(err => err)
        if (err === null && succ.errMsg === 'chooseAddress:ok') {
          this.updateAddress(succ)
        }
      }
    }
	}
</script>
<style lang="scss">
.address-img{
  display: block;
  width: 100%;
  height: 5px;
}
.goods-address-box{
  display: flex;
  justify-content: center;
  height: 90px;
  align-items: center;
  .address-btn{
    
  }
}
.address-info-box{
  font-size: 14px;
  padding: 0 5px;
  .row1{
    display: flex;
    justify-content: space-between;
    .row1-username{}
    .row1-phone{}
  }
  .row2{
    margin-top: 10px;
    .row2-address{
      display: flex;
      align-items: center;
      .row2-left{
        white-space: nowrap;
      }
      .row2-right{
        margin-left: 10px;
      }
    }
  }
}
</style>
